package com.sandking.builder.start;

import java.sql.Connection;
import java.util.List;

import com.sandking.builder.factory.ConnectionFactory;
import com.sandking.builder.factory.FileWriterFactory;
import com.sandking.builder.pojo.DataSourceConfig;
import com.sandking.builder.pojo.Table;
import com.sandking.builder.util.TableUtil;

import freemarker.template.Configuration;

public class Start {
	private String sqlMapPackage = "db";
	private String pojoPackage = "db.pojo";
	private String daoPackage = "db.dao";
	private String impPackage = "db.imp";
	private String extPackage = "db.ext";
	private String xmlPackage = "db.xml";
	private String xmlURL = "db/xml";
	private String extjsPackage = "db.extjs";

	private String templateUrl = "/com/sandking/builder/template/";
	private DataSourceConfig cfg;

	public Start(String sqlMapPackage, String pojoPackage, String daoPackage,
			String impPackage, String extPackage, String xmlPackage,
			String xmlURL, String templateUrl, DataSourceConfig cfg) {
		super();
		this.sqlMapPackage = sqlMapPackage;
		this.pojoPackage = pojoPackage;
		this.daoPackage = daoPackage;
		this.impPackage = impPackage;
		this.extPackage = extPackage;
		this.xmlPackage = xmlPackage;
		this.xmlURL = xmlURL;
		this.templateUrl = templateUrl;
		this.cfg = cfg;
	}

	public void pay(boolean pojo, boolean dao, boolean imp, boolean ext,
			boolean xml) throws Exception {
		Connection conn = ConnectionFactory.getConnection(cfg);
		List<Table> tables = TableUtil.getTables(conn, sqlMapPackage,
				pojoPackage, daoPackage, impPackage, extPackage, xmlPackage,
				xmlURL);
		Configuration configuration = FileWriterFactory
				.getConfiguration(templateUrl);
		for (Table table : tables) {
			if (pojo) {
				outPojo(table, configuration);
			}
			if (dao) {
				outDao(table, configuration);
			}
			if (imp) {
				outImp(table, configuration);
			}
			if (ext) {
				outExt(table, configuration);
			}
			if (cfg.getDataSourceType() != 0 && xml) {
				outXml(table, cfg, configuration);
			}
			
			outExtjs(table, cfg, configuration);

		}
		for (Table table : tables) {
			System.out.println(String.format("<sqlMap resource=\"%s\"/>", table
					.getXmlUrl()
					+ "/" + table.getClassName_d() + ".xml"));

		}
	}

	/**
	 * 生成POJO对象
	 * 
	 * @param table
	 */
	public void outPojo(Table table, Configuration configuration) {
		FileWriterFactory.dataSourceOut(configuration, "pojo.ftl", table, table
				.getClassName_d()
				+ ".java", FileWriterFactory.POJO);
	}

	/**
	 * 生成DAO对象
	 * 
	 * @param table
	 */
	public void outDao(Table table, Configuration configuration) {
		FileWriterFactory.dataSourceOut(configuration, "dao.ftl", table, table
				.getClassName_d()
				+ "Service.java", FileWriterFactory.DAO);
	}

	/**
	 * 生成IMP对象
	 * 
	 * @param table
	 */
	public void outImp(Table table, Configuration configuration) {
		FileWriterFactory.dataSourceOut(configuration, "imp.ftl", table, table
				.getClassName_d()
				+ "ServiceImp.java", FileWriterFactory.IMP);
	}

	/**
	 * 生成EXT对象
	 * 
	 * @param table
	 */
	public void outExt(Table table, Configuration configuration) {
		FileWriterFactory.dataSourceOut(configuration, "ext.ftl", table, table
				.getClassName_d()
				+ "Manage.java", FileWriterFactory.EXT);
	}

	/**
	 * 生成xml文件
	 * 
	 * @param table
	 */
	public void outXml(Table table, DataSourceConfig cfg,
			Configuration configuration) {
		String fileName = "";
		switch (cfg.getDataSourceType()) {
		case DataSourceConfig.MYSQL:
			fileName = "ibatis_mysql.ftl";
			break;
		case DataSourceConfig.SQLSERVER:
			fileName = "ibatis_sqlserver.xml";
			break;
		}

		FileWriterFactory.dataSourceOut(configuration, fileName, table, table
				.getClassName_d()
				+ ".xml", FileWriterFactory.XML);
	}
	
	/**
	 * 生成Extjs文件
	 * 
	 * @param table
	 */
	public void outExtjs(Table table, DataSourceConfig cfg,
			Configuration configuration) {
		String fileName = "";
		switch (cfg.getDataSourceType()) {
		case DataSourceConfig.MYSQL:
			fileName = "ext_grid.ftl";
			break;
		case DataSourceConfig.SQLSERVER:
			fileName = "ext_grid.xml";
			break;
		}

		FileWriterFactory.dataSourceOut(configuration, fileName, table, table
				.getClassName_d()
				+ ".js", 6);
	}
}
